﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ict.DataAccess.Conditions
{
    public class LogicCond : Condition
    {
        /// <summary>
        /// 定义逻辑运算符的枚举
        /// </summary>
        public enum LogicOperator
        {
            /// <summary>
            /// 与
            /// </summary>
            And,
            /// <summary>
            /// 或
            /// </summary>
            Or,
            /// <summary>
            /// 非
            /// </summary>
            Not
        }

        private LogicOperator _operator = LogicOperator.And;
        /// <summary>
        /// 获取逻辑运算符
        /// </summary>
        public LogicOperator Operator
        {
            get
            {
                return _operator;
            }
        }

        private List<Condition> _conditions = new List<Condition>();
        /// <summary>
        /// 获取条件集合
        /// </summary>
        public List<Condition> Conditions
        {
            get
            {
                return _conditions;
            }
        }

        /// <summary>
        /// 生成逻辑关系为与的条件
        /// </summary>
        /// <param name="conditions">参与运算的条件</param>
        public LogicCond(params Condition[] conditions)
        {
            if (conditions.Length > 0)
            {
                if (_conditions == null)
                    _conditions = new List<Condition>();
                foreach (Condition c in conditions)
                {
                    _conditions.Add(c);
                }
            }
        }

        /// <summary>
        /// 生成任意逻辑关系条件
        /// </summary>
        /// <param name="op">逻辑运算符</param>
        /// <param name="conditions">参与运算的条件</param>
        public LogicCond(LogicOperator op, params Condition[] conditions)
        {
            if (op == LogicOperator.Not && conditions.Length > 1)
                throw new Exception("Not operator cannot take more than one operand.");
            _operator = op;
                
            foreach (Condition c in conditions)
            {
                _conditions.Add(c);
            }
        }

        public void Add(Condition condition)
        {
            if (condition != null)
                _conditions.Add(condition);
            else
                throw new Exception("No conditions are provided.");
        }
    }
}
